/**
 * Created by jiaxing on 2017/11/16.
 */
$(function () {
    var ChenJi = "";
    //初始化用户名
    $("#user").html(sessionStorage.getItem("username"));
    var date = new Date();
    var time = date.getFullYear()+"-"+11+"-"+date.getDate();
    $("#time").html(time);


    //初始化学生信息的请求


        var obj = {
            url:"/shixun/stu2?id="+sessionStorage.getItem("username"),
            dataType:"json",
            type:"get",
            success:function(data){
                var Data = data[0];
                console.log(Data);

                $("#Student11_StudentNo").val(Data.id);
                $("#Student11_StudentName").val(Data.name);
                $("#Student11_NamePY").val(Data.namel);
                //性别数据转化
                var sex = (Data.sex=="男")?1:0;
                $("#Student11_Sex input[value="+ sex +"]").attr("checked","checked");
                $("#Student11_BirthDay").val(Data.birthday);
                $("#Student11_GuideTeacher").val(Data.tutor);
                $("#Student11_InTime").val(Data.cra);
                $("#Student11_OutTime").val(Data.grade);

                //是否残疾判断
                var canji = "";
                if(Data.lame=="残疾"){
                    canji = 1;
                }else{
                    canji = 0;
                }
                $("#Student11_IsDeformity input[value="+ canji +"]").attr("checked","checked");


                $("#Student11_NativePlace").val(Data.pla);
                $("#Student21_IdCard").val(Data.idcard);
                //户口类型判断
                var hukou = "";
                if(Data.node=="农村"){
                    hukou = 1;
                }else{
                    hukou = 2;
                }
                $("#Student21_FamillyReg input[value =  "+ hukou +" ]").attr("checked","checked");
                // $("#Student21_FamillyReg input[value="+ Data.nodetype +""]").attr("checked",true);
                // $("#Student21_ExamineeType").val(Data.id);
                // $("#Student21_GetType1").val(Data.id);
                $("#Student21_ExamineeNo").val(Data.ad);
                $("#Student21_UnitSchool").val(Data.pre);
                $("#Student21_DoctorNo").val(Data.medcard);
                // $("#Student21_BankName").val(Data.id);
                $("#Student21_BankNo").val(Data.bankid);
                // $("#Student21_MainForLang").val(Data.id);
                // $("#Student21_IsOtherSpecialty_0").val(Data.id);
                // $("#Student21_IsSecondDegree_0").val(Data.id);
                $("#Student21_hobby").val(Data.hobby);
                $("#Student21_Strengths").val(Data.spe);
                $("#Student21_FamillyCount").val(Data.family);

            },
            error:function(e){
                console.log(e);
            }
        }
        $.ajax(obj);


    //侧边栏点击事件
    var lis = $(".menu_li")
    console.log(lis);
    lis.click(function () {
        //console.log($(this).attr("data_name"));
        var current = $(this).attr("data_name");
        $(".content ul li").css("display","none");
        $("."+current).css("display","block");

        //ajax请求

        //学生信息
        if(current == "content_info"){
            var obj = {
                url:"/shixun/stu2?id="+sessionStorage.getItem("username"),
                dataType:"json",
                type:"get",
                success:function(data){
                    var Data = data[0];
                    console.log(Data);
                },
                error:function(e){
                    console.log(e);
                }
            }
            $.ajax(obj);
        }
        //课表
        else if(current == "content_schedule")
        {
            var obj = {
                url:"/shixun/stu4?id="+sessionStorage.getItem("username"),
                dataType:"json",
                type:"get",
                success:function(data){
                    console.log(data);
                    for(var i=0;i<data.length;i++){
                        if(data[i].ctime=="星期一第一讲"){
                            $(".c11").html(data[i].cname);
                        }else if(data[i].ctime=="星期一第二讲"){
                            $(".c12").html(data[i].cname);
                        }else if(data[i].ctime=="星期一第三讲"){
                            $(".c13").html(data[i].cname);
                        }else if(data[i].ctime=="星期一第四讲"){
                            $(".c14").html(data[i].cname);

                        }else if(data[i].ctime=="星期二第一讲"){
                            $(".c21").html(data[i].cname);
                        }else if(data[i].ctime=="星期二第二讲"){
                            $(".c22").html(data[i].cname);
                        }else if(data[i].ctime=="星期二第三讲"){
                            $(".c23").html(data[i].cname);
                        }else if(data[i].ctime=="星期二第四讲"){
                            $(".c24").html(data[i].cname);

                        }else if(data[i].ctime=="星期三第一讲"){
                            $(".c31").html(data[i].cname);
                        }else if(data[i].ctime=="星期三第二讲"){
                            $(".c32").html(data[i].cname);
                        }else if(data[i].ctime=="星期三第三讲"){
                            $(".c33").html(data[i].cname);
                        }else if(data[i].ctime=="星期三第四讲"){
                            $(".c34").html(data[i].cname);

                        }else if(data[i].ctime=="星期四第一讲"){
                            $(".c41").html(data[i].cname);
                        }else if(data[i].ctime=="星期四第二讲"){
                            $(".c42").html(data[i].cname);
                        }else if(data[i].ctime=="星期四第三讲"){
                            $(".c43").html(data[i].cname);
                        }else if(data[i].ctime=="星期四第四讲"){

                            $(".c44").html(data[i].cname);
                        }else if(data[i].ctime=="星期五第一讲"){
                            $(".c51").html(data[i].cname);
                        }else if(data[i].ctime=="星期五第二讲"){
                            $(".c52").html(data[i].cname);
                        }else if(data[i].ctime=="星期五第三讲"){
                            $(".c53").html(data[i].cname);
                        }else if(data[i].ctime=="星期五第四讲"){
                            $(".c54").html(data[i].cname);
                        }
                    }
                },
                error:function(e){
                    console.log(e);
                }
            }
            $.ajax(obj);
        }
        //成绩单
        else if(current == "content_report"){
            var obj = {
                url:"/shixun/stu3?id="+sessionStorage.getItem("username"),
                dataType:"json",
                type:"get",
                success:function(data){
                    ChenJi = data;
                console.log(data);
                    var trs = $(".chenji");
                    for(var i=0;i<data.length;i++){
                        var tds = trs.eq(i).children();
                        tds.eq(0).html(data[i].cname);
                        tds.eq(1).html(data[i].cid);
                        tds.eq(2).html(data[i].cgpa);
                        tds.eq(3).html(data[i].cnature);
                        if(data[i].score=="404"){
                            tds.eq(4).html("成绩还未发布");
                            tds.eq(6).html("");
                        }
                        else{
                            tds.eq(4).html(data[i].score);
                            tds.eq(6).html(1+(4-Math.ceil((100-data[i].score)/10)));
                        }
                        if(data[i].bukao=="404"){
                            tds.eq(5).html("");
                        }else{
                            tds.eq(5).html(data[i].bukao);
                        }
                    }
                },
                error:function(e){
                    console.log(e);
                }
            }
            $.ajax(obj);
        }

    })
    $(".input_txt").focus(function () {
        $(this).attr("value","");
    })




    //查询成绩
    $("#search").click(function () {
        var course =$(".input_txt").val();
        $(".chenji td").html("");
        var flag = false;
        for(var k in ChenJi){
            if(ChenJi[k].cname == course){
                flag = true;
                var tds = $(".first").children();
                tds.eq(0).html(ChenJi[k].cname);
                tds.eq(1).html(ChenJi[k].cid);
                tds.eq(2).html(ChenJi[k].ccredit);
                tds.eq(3).html(ChenJi[k].cnature);
                if(ChenJi[k].score=="404"){
                    tds.eq(4).html("成绩还未发布");
                    tds.eq(6).html("");
                }
                else{
                    tds.eq(4).html(ChenJi[k].score);
                    tds.eq(6).html(1+Math.ceil((100-ChenJi[k].score)/10));
                }


            }

        }
        if(flag==false){
            alert("没有查询到相关成绩！");
        }
    })

    //修改密码

    $("#Save").click(function () {
        console.log("修改密码");
        var newpass = $("#NewPass").val();
        var new2 = $("#NewPassAgin").val();
        if(newpass==new2){
            var obj = {
                url:"/shixun/stu5?id="+sessionStorage.getItem("username")+"&pass="+newpass,
                dataType:"json",
                type:"get",
                success:function(data){
                   if(data.result=="true"){
                       alert("修改成功！")
                   }
                    else{
                       alert("修改失败！");
                   }
                },
                error:function(e){
                    console.log(e);
                }
            }
            $.ajax(obj);
        }
        else{
            alert("两次输入密码不一致！");
        }


    })
    //修改信息
    $("#submit").click(function () {
        console.log("修改信息");
        var id = sessionStorage.getItem("username");
        var like = $("#Student21_hobby").val();
        var techang = $("#Student21_Strengths").val();

        var obj = {
            url:"/shixun/stu6?id="+id+"&Student21$hobby="+ like + "&Student21$Strengths="+techang,
            dataType:"json",
            type:"get",
            success:function(data){
                console.log(data);
                if(data.result=="true"){
                    alert("修改成功！")
                }
                else{
                    alert("修改失败！");
                }
            },
            error:function(e){
                console.log(e);
            }
        }
        $.ajax(obj);

    })

})
